package com.nextplus.housetolet.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nextplus.housetolet.domain.Room;
import com.nextplus.housetolet.domain.User;

/**
 * 房间管理
 * 
 * @author didi
 *
 * 创建时间：2013-1-8
 *
 */
@Controller
public class RoomController extends BaseController {
	
	@Transactional
	@RequestMapping(value = "/room/list", method = RequestMethod.GET) 
	public String roomList(
			HttpSession session, 
			ModelMap modelMap) {
		User user = (User) session.getAttribute("user");
		if(user == null) {
			return "redirect:/login";
		}
		user = userRepository.findOne(user.getId());
		List<Room> rooms = roomRepository.findByUser(user);
		modelMap.addAttribute("roomList", rooms);
		modelMap.addAttribute(PAGE_PATH, "content/room/roomList");
		return "layout";
	}
	
	@RequestMapping(value = "/room/del/{roomId}", method = RequestMethod.GET) 
	public String roomDel(
			@PathVariable("roomId") Long roomId,
			HttpSession session, 
			ModelMap modelMap) {
		User user = (User) session.getAttribute("user");
		if(user == null) {
			return "redirect:/login";
		}
		roomRepository.delete(roomId);
		modelMap.addAttribute(PAGE_PATH, "content/room/list");
		return "redirect://room/list";
	}
	
	@RequestMapping(value = {"/room/create"}, method = RequestMethod.GET) 
	public String roomCreateForm(
			HttpSession session, 
			ModelMap modelMap) {
		User sessionUser = (User) session.getAttribute("user");
		if(sessionUser == null) {
			return "redirect:/login";
		}
		modelMap.addAttribute(PAGE_PATH, "content/room/roomCreateForm");
		modelMap.addAttribute("room", new Room());
		return "layout";
	}
	
	@RequestMapping(value = "/room/create", method = RequestMethod.POST) 
	public String roomCreate(
			@ModelAttribute("room") Room room, 
			HttpSession session,
			Errors errors, 
			ModelMap modelMap) {
		User sessionUser = (User) session.getAttribute("user");
		if(sessionUser == null) {
			return "redirect:/login";
		}
		if(errors.hasErrors()) {
			modelMap.addAttribute(PAGE_PATH, "content/room/roomCreateForm");
			return "layout";
		} 
		User user = userRepository.findOne(sessionUser.getId());
		Room newRoom = new Room();
		newRoom.setUser(user);
		newRoom.setName(room.getName());
		roomRepository.save(newRoom);
		modelMap.addAttribute(PAGE_PATH, "content/room/list");
		return "redirect://room/list";
	}
	
	@RequestMapping(value = {"/room/update/{roomId}"}, method = RequestMethod.GET) 
	public String roomForm(
			@PathVariable("roomId") Long roomId,
			HttpSession session, 
			ModelMap modelMap) {
		User sessionUser = (User) session.getAttribute("user");
		if(sessionUser == null) {
			return "redirect:/login";
		}
		modelMap.addAttribute(PAGE_PATH, "content/room/roomUpdateForm");
		modelMap.addAttribute("room", roomRepository.findOne(roomId));
		return "layout";
	}
	
	@RequestMapping(value = "/room/update/{roomId}", method = RequestMethod.POST) 
	public String roomCreate(
			@PathVariable("roomId") Long roomId,
			@ModelAttribute("room") Room room, 
			HttpSession session, 
			Errors errors, 
			ModelMap modelMap) {
		User sessionUser = (User) session.getAttribute("user");
		if(sessionUser == null) {
			return "redirect:/login";
		}
		if(errors.hasErrors()) {
			modelMap.addAttribute(PAGE_PATH, "content/room/roomUpdateForm");
			return "layout";
		} 
		User user = userRepository.findOne(sessionUser.getId());
		Room newRoom = roomRepository.findOne(roomId);
		newRoom.setUser(user);
		newRoom.setName(room.getName());
		roomRepository.save(newRoom);
		modelMap.addAttribute(PAGE_PATH, "content/room/list");
		return "redirect://room/list";
	}

}
